#include "Testing.h"

char redirection = 0;       /* Whether or not redirection is enabled. */
char buffer[1000] = {0};    /* Temporarily stores input from the user. */
char command[1000] = {0};   /* The command entered by the user. */
char arg[10][100] = {0};   /* The arguments of the command. */
char argc = 0;              /* The number of arguments provided. */
char comment[1000] = {0};   /* A comment entered by the user. */


int main(){
    while(1){
        int i = 0;

        printf(">>> ");
        readCommand();
        if(redirection == 1){
            printf("%s\n", buffer);
        }
        runCommand();
    }
}

void clearScreen(){
#ifdef _WIN32
    system("cls");
#else
    system("clear");
#endif
}

void readCommand(){
    int i, j, k;

    /* Get a string from the user.*/
    while(fgets(buffer, 1000, stdin) == 0);

    /* If the command is a comment, don't parse it. */
    if(buffer[0] == '*' || buffer[0] == '\n' || buffer[0] == '!'){
        command[0] = '*';
        command[1] = '\0';
        for(i = 1; i < strlen(buffer); i++){
            comment[i-1] = buffer[i];
        }
        comment[i] = '\0';
        return;
    }

    /* Normalize the data. */
    for(i = 0; i < strlen(buffer); i++){
        buffer[i] = tolower(buffer[i]);
    }
    buffer[strlen(buffer)-1] = '\0';
    argc = 0;

    /* Parse the command. */
    for(i = 0; i < strlen(buffer); i++){
        if(buffer[i] == ' '){
            break;
        }
        command[i] = buffer[i];
    }
    command[i] = '\0';
    i++;

    /* If the command is not syscall, then just parse the remaining arguments. */
    for(j = 0; j < 10, i < strlen(buffer); j++){
        for(k = 0; i < strlen(buffer), k < 100; i++, k++){
            if(buffer[i] == ' '){
                break;
            }
            if(buffer[i] != ','){
                arg[j][k] = buffer[i];
            }
        }
        arg[j][k] = '\0';
        argc++;
        i++;
    }

    return;
}